/*
 * 1-wire.h
 *
 *  Created on: 01.12.2012
 *      Author: jbolay
 */

#ifndef ONE_WIRE_H
#define ONE_WIRE_H

#include <avr/io.h>
#include <util/delay.h>

#define F_CPU 4915200UL



#define LOOP_CYCLES 8 //Number of cycles that the loop takes
#define us(num) (num/(LOOP_CYCLES*(1/(F_CPU/1000000.0))))

#define	ONE_PORT	PORTC
#define	ONE_DDR		DDRC
#define	ONE_PIN		PINC
#define	ONE_PORTPIN	PC1

#define ONE_INPUT_MODE() 	ONE_DDR &= ~(1 << ONE_PORTPIN)
#define ONE_OUTPUT_MODE()	ONE_DDR |= (1 << ONE_PORTPIN)
#define ONE_LOW()			ONE_PORT &= ~(1 << ONE_PORTPIN)
#define ONE_HIGH()			ONE_PORT|= (1 << ONE_PORTPIN)
#define ONE_READ()			ONE_PIN & (1 << ONE_PORTPIN)

#define THERM_CMD_CONVERTTEMP 0x44
#define THERM_CMD_RSCRATCHPAD 0xbe
#define THERM_CMD_WSCRATCHPAD 0x4e
#define THERM_CMD_CPYSCRATCHPAD 0x48
#define THERM_CMD_RECEEPROM 0xb8
#define THERM_CMD_RPWRSUPPLY 0xb4
#define THERM_CMD_SEARCHROM 0xf0
#define THERM_CMD_READROM 0x33
#define THERM_CMD_MATCHROM 0x55
#define THERM_CMD_SKIPROM 0xcc
#define THERM_CMD_ALARMSEARCH 0xec

int one_init();
void one_write_bit(uint8_t bit);
uint8_t one_read_bit();
void one_write_byte(uint8_t byte);
uint8_t one_read_byte();
int16_t raw_to_decicelsius(uint16_t measure);
uint16_t format_from_deci( int16_t decicelsius, char str[]);
uint8_t read_temperatur(char measureString[], uint16_t measure);

#endif /* 1_WIRE_H_ */
